package com.ct.controller.sysuser;

import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.sysuser.model.vo.SysUserDetailVO;
import com.ct.application.sysuser.request.*;
import com.ct.application.sysuser.service.ISystemUserService;
import com.ct.commons.BaseController;
import com.ct.commons.annotation.ApiLog;
import com.ct.commons.annotation.HasAdmin;
import com.ct.commons.annotation.PermissionApi;
import com.ct.commons.aspect.limiter.AccessIpLimiter;
import com.ct.repository.commons.PageUtils;
import com.ct.utils.result.DeletedEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * RBAC-用户管理
 */
@RestController
@PermissionApi(title = "用户管理模块",path="/api/v1/sysuser",code = "M1000",sorted = 12)
public class AdminSysUserController extends BaseController {

    @Autowired
    private ISystemUserService systemUserService;

    /**
     * 根据系统查询租户用户列表
     * @param sysUserSearchRequest
     * @return
     */
    @PostMapping("/sysuser/system/list")
    @PermissionApi(title = "租户用户列表",path="/api/v1/sysuser/system/list",code = "M1001",sorted = 1)
    public PageUtils<SysUserDetailVO> findUserListBySystemId(@Validated @RequestBody SysUserSearchRequest sysUserSearchRequest){
        sysUserSearchRequest.setIsDeleted(DeletedEnums.UN_DELETED.getCode());
        return systemUserService.findUserListBySystemId(sysUserSearchRequest);
    }


    /**
     * 查询后台用户列表
     * @param sysUserSearchRequest
     * @return
     */
    @PostMapping("/sysuser/list")
    @PermissionApi(title = "查询后台用户列表",path="/api/v1/sysuser/list",code = "M1002",sorted = 2)
    public PageUtils<SysUserDetailVO> pageSysUsers(@Validated @RequestBody SysUserSearchRequest sysUserSearchRequest){
        sysUserSearchRequest.setIsDeleted(DeletedEnums.UN_DELETED.getCode());
        return systemUserService.findPage(sysUserSearchRequest);
    }

    /**
     * 回收站
     * @param sysUserSearchRequest
     * @return
     */
    @PostMapping("/sysuser/recycle")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "回收站",path="/api/v1/sysuser/recycle",code = "M1003",sorted = 3)
    public PageUtils<SysUserDetailVO> recycleSysUsers(@Validated @RequestBody SysUserSearchRequest sysUserSearchRequest){
        sysUserSearchRequest.setIsDeleted(DeletedEnums.DELETED.getCode());
        return systemUserService.findPage(sysUserSearchRequest);
    }

    /**
     * 保存后台用户
     * @param sysUserSaveRequest
     * @return
     */
    @PostMapping("/sysuser/save")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("保存后台用户")
    @PermissionApi(title = "保存后台用户",path="/api/v1/sysuser/save",code = "M1004",sorted = 4)
    public SysUserDetailVO saveSysUsers(@Validated @RequestBody SysUserSaveRequest sysUserSaveRequest){
        return systemUserService.saveSysUsers(sysUserSaveRequest);
    }

    /**
     * 保存租户用户
     * @param sysUserSaveRequest
     * @return
     */
    @PostMapping("/sysuser/system/save")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("保存租户用户")
    @PermissionApi(title = "保存租户用户",path="/api/v1/sysuser/system/save",code = "M1005",sorted = 5)
    public SysUserDetailVO saveSystemSysUsers(@Validated @RequestBody SysUserSaveRequest sysUserSaveRequest){
        return systemUserService.saveSystemSysUsers(sysUserSaveRequest);
    }


    /**
     * 修改后台用户
     * @param sysUserSaveRequest
     * @return
     */
    @PostMapping("/sysuser/update")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("修改后台用户")
    @PermissionApi(title = "修改后台用户",path="/api/v1/sysuser/update",code = "M1006",sorted = 6)
    public SysUserDetailVO updateSysUsers(@Validated @RequestBody SysUserUpdateRequest sysUserSaveRequest){
        return systemUserService.updateSysUsers(sysUserSaveRequest);
    }

    /**
     * 根据ID查询后台用户
     * @param id
     * @return
     */
    @PostMapping("/sysuser/get/{id}")
    @PermissionApi(title = "根据ID查询后台用户",path="/api/v1/sysuser/get/{id}",code = "M1007",sorted = 7)
    public SysUserDetailVO getSysUsers(@PathVariable("id") Long id){
        return systemUserService.selectById(id);
    }


    /**
     * 删除用户
     * @param id
     * @return
     */
    @PostMapping("/sysuser/del/{id}")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("删除用户")
    @PermissionApi(title = "删除用户",path="/api/v1/sysuser/del/{id}",code = "M1008",sorted = 8)
    public Boolean delSysUsers(@PathVariable("id") Long id){
        return systemUserService.deleteById(id);
    }

    /**
     * 恢复删除
     * @param id
     * @return
     */
    @PostMapping("/sysuser/recover/{id}")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("恢复删除")
    @PermissionApi(title = "恢复删除",path="/api/v1/sysuser/recover/{id}",code = "M1009",sorted = 9)
    public Boolean recoverById(@PathVariable("id") Long id){
        return systemUserService.recoverById(id);
    }


    /**
     * 根据字段修改状态
     * @param statusRequest
     * @return
     */
    @PostMapping("/sysuser/update/status")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("修改状态")
    @PermissionApi(title = "修改状态",path="/api/v1/sysuser/update/status",code = "M1010",sorted = 10)
    public Boolean updateStatusSysUsers(@Validated @RequestBody StatusRequest statusRequest){
        return systemUserService.updateStatusSysUsers(statusRequest);
    }

    /**
     * 修改密码
     * @param sysUserPasswordRequest
     * @return
     */
    @PostMapping("/sysuser/update/password")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("修改密码")
    @PermissionApi(title = "修改密码",path="/api/v1/sysuser/update/password",code = "M1011",sorted = 11)
    public Boolean updatePassword(@Validated @RequestBody SysUserPasswordRequest sysUserPasswordRequest){
        return systemUserService.updatePassword(sysUserPasswordRequest);
    }

    /**
     * 绑定角色
     * @param sysUserBindRolesRequest
     * @return
     */
    @PostMapping("/sysuser/bindroles")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("绑定角色")
    @PermissionApi(title = "绑定角色",path="/api/v1/sysuser/bindroles",code = "M1012",sorted = 12)
    public Boolean bindRolesSysUsers(@Validated @RequestBody SysUserBindRolesRequest sysUserBindRolesRequest){
        return systemUserService.bindRolesSysUsers(sysUserBindRolesRequest);
    }

    /**
     * 删除用户--物理删除
     * @param id
     * @return
     */
    @PostMapping("/sysuser/remove/{id}")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("删除用户--物理删除")
    @PermissionApi(title = "删除用户--物理删除",path="/api/v1/sysuser/remove/{id}",code = "M1013",sorted = 13)
    public Boolean removeById(@PathVariable("id") Long id){
        return systemUserService.removeById(id);
    }


    /**
     * 批量删除--物理删除
     * @param sysUserBatchRequest
     * @return
     */
    @PostMapping("/sysuser/batch/removes")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("批量删除--物理删除")
    @PermissionApi(title = "批量删除--物理删除",path="/api/v1/sysuser/batch/removes",code = "M1014",sorted = 14)
    public Boolean batchRemoves(@Validated @RequestBody SysUserBatchRequest sysUserBatchRequest){
        return systemUserService.batchRemoves(sysUserBatchRequest);
    }


    /**
     * 批量删除
     * @param sysUserBatchRequest
     * @return
     */
    @PostMapping("/sysuser/batch/dels")
    @ApiLog("批量删除")
    @PermissionApi(title = "批量删除",path="/api/v1/sysuser/batch/dels",code = "M1015",sorted = 15)
    public Boolean batchDels(@Validated @RequestBody SysUserBatchRequest sysUserBatchRequest){
        return systemUserService.batchDels(sysUserBatchRequest);
    }

    /**
     * 批量恢复删除
     * @param sysUserBatchRequest
     * @return
     */
    @PostMapping("/sysuser/batch/recovers")
    @ApiLog("批量恢复删除")
    @PermissionApi(title = "批量恢复删除",path="/api/v1/sysuser/batch/recovers",code = "M1016",sorted = 16)
    public Boolean batchRecovers(@Validated @RequestBody SysUserBatchRequest sysUserBatchRequest){
        return systemUserService.batchRecovers(sysUserBatchRequest);
    }

    /**
     * 批量通过
     * @param sysUserBatchRequest
     * @return
     */
    @ApiLog("批量通过")
    @PostMapping("/sysuser/batch/statuss")
    @PermissionApi(title = "批量通过",path="/api/v1/sysuser/batch/statuss",code = "M1017",sorted = 17)
    public Boolean batchStatus(@Validated @RequestBody SysUserBatchRequest sysUserBatchRequest){
        return systemUserService.batchStatus(sysUserBatchRequest);
    }

    /**
     * 批量禁止
     * @param sysUserBatchRequest
     * @return
     */
    @ApiLog("批量禁止")
    @PostMapping("/sysuser/batch/forbiddens")
    @PermissionApi(title = "批量禁止",path="/api/v1/sysuser/batch/forbiddens",code = "M1018",sorted = 18)
    public Boolean batchForbiddens(@Validated @RequestBody SysUserBatchRequest sysUserBatchRequest){
        return systemUserService.batchForbiddens(sysUserBatchRequest);
    }


}
